/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef __DRAWINGBUFFER_H__
#define __DRAWINGBUFFER_H__

#include "conuidef.h"
//#include "conuisurface.h"
#include <e32base.h>

class MDrawer
    {
public:
    virtual ConUiDef::TColor Background() const = 0;
    virtual void SetBackground(ConUiDef::TColor aColor) = 0;
    virtual ConUiDef::TColor Foreground() const = 0;
    virtual void SetForeground(ConUiDef::TColor aColor) = 0;
    virtual TBool Bold() const = 0;
    virtual void SetBold(TBool aBold) = 0;
    virtual TBool InverseVideo() const = 0;
    virtual void SetInverseVideo(TBool aInverseVideo) = 0;
    virtual TPoint Pos() const = 0;
    virtual void SetPos(const TPoint& aPos) = 0;
    virtual void Write(const TDesC& aOutput) = 0;
    };

NONSHARABLE_CLASS(CDrawingBuffer) : public CBase
    {
public:
    enum TBlinkMode
        {
        EBlinkInvisible,
        EBlinkVisible
        };
    static CDrawingBuffer* NewL(const TSize& aSize);
    inline const TRect& Rect() const;
    inline TBlinkMode BlinkMode() const;
    inline void SetBlinkMode(TBlinkMode aMode);
    void UpdateDrawerL(MDrawer& aDrawer, const RRegion& aRegion) const;
    void FillBackground(const TRect& aRect, ConUiDef::TColor aColor);
    void FillForeground(const TRect& aRect, ConUiDef::TColor aColor);
    void FillBold(const TRect& aRect, TBool aBold);
    void FillBlinking(const TRect& aRect, TBool aBlinking);
    void FillInverseVideo(const TRect& aRect, TBool aInverseVideo);
    void FillText(const TRect& aRect, TText aText);
    void FillText(const TPoint& aPos, const TDesC& aText);
    void ClearText(const TRect& aRect);
    ConUiDef::TColor Background(const TPoint& aPos) const;
    ConUiDef::TColor Foreground(const TPoint& aPos) const;
    TBool Bold(const TPoint& aPos) const;
    TBool Blinking(const TPoint& aPos) const;
    TBool InverseVideo(const TPoint& aPos) const;
    TText Text(const TPoint& aPos) const;
    TPtrC Text(const TPoint& aPos, TInt aLength) const;
private:
    CDrawingBuffer(const TSize& aSize);
    TUint8* ForegroundArray(const TPoint& aPos);
    const TUint8* ForegroundArray(const TPoint& aPos) const;
    TUint8* BackgroundArray(const TPoint& aPos);
    const TUint8* BackgroundArray(const TPoint& aPos) const;
    TUint8* BoldArray(const TPoint& aPos);
    const TUint8* BoldArray(const TPoint& aPos) const;
    TUint8* BlinkingArray(const TPoint& aPos);
    const TUint8* BlinkingArray(const TPoint& aPos) const;
    TUint8* InverseVideoArray(const TPoint& aPos);
    const TUint8* InverseVideoArray(const TPoint& aPos) const;
    TText* TextArray(const TPoint& aPos);
    const TText* TextArray(const TPoint& aPos) const;
    inline TInt Offset(const TPoint& aPos) const;
private:
    TRect iRect;
    TInt iArea;
    TBlinkMode iBlinkMode;
    };
///////////////////////////////////////////////////////////////////////////////
inline const TRect& CDrawingBuffer::Rect() const
    {
    return iRect;
    }

inline CDrawingBuffer::TBlinkMode CDrawingBuffer::BlinkMode() const
    {
    return iBlinkMode;
    }

inline void CDrawingBuffer::SetBlinkMode(CDrawingBuffer::TBlinkMode aMode)
    {
    iBlinkMode = aMode;
    }

inline TInt CDrawingBuffer::Offset(const TPoint& aPos) const
    {
    return aPos.iY*iRect.Width() + aPos.iX;
    }

#endif /* __DRAWINGBUFFER_H__ */
